<template>
  <div class="updata-genden">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      :default-index="value"
      @confirm="onComfirm"
      @cancel="$emit('close')"
      @change="onChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdataGender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      localGrnder: 0
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    onChange (picker, value, index) {
      this.localGrnder = index
    },
    async onComfirm () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true
      })

      const localGrnder = this.localGrnder
      try {
        await updateUserProfile({
          gender: localGrnder
        })
        // 传值到父
        this.$emit('input', localGrnder)
        // 关闭弹出层
        this.$emit('close')
        this.$toast('更新成功')
      } catch (error) {
        this.$toast('更新失败')
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
